// import index from 'wis/index.js'

let roadname = '';
export default class WarnHandler {
    constructor() {
        this._marker = null;
        this._popup = null;
        this._moveFeature = null
        this._clickFeature = null
    }

    setVisible(map) {
        if (map.solution == 3756) {
            map.setLayoutProperty('warn', 'visibility', 'visible');
        }
        if (map.solution == 4389) {
            map.setLayoutProperty('warn', 'visibility', 'visible');
        }
        // map.setLayoutProperty('5a3ea454892d4824b622cdabb48950a2', 'visibility', 'visible');
        this._clear(map);
    }

    setUnvisible(map) {
        if (map.solution == 3756) {
            map.setLayoutProperty('warn', 'visibility', 'none');
        }
        if (map.solution == 4389) {
            map.setLayoutProperty('warn', 'visibility', 'none');
        }
        // map.setLayoutProperty('5a3ea454892d4824b622cdabb48950a2', 'visibility', 'none');
        this._clear(map);
    }

    roadHandler(lng, lat, radius) {
        console.log(lan,lat)
        minemap.util.getJSON('http://203.81.245.38:48082/weather/RoadNameSearch/{lng}/{lat}/{radius}')
    }

    onMouseClickWarnHandler (map, feature, e){
        if (!map) {
            return;
        }
        if (this._popup) {
            this._popup.remove();
            this._popup = null;
        }
        if (this._marker) {
            this._marker.remove();
            this._marker = null;
        }
        if (!feature) {
            this._clickFeature = null;
        } else {
            this._moveFeature = null;
            this._clickFeature = feature;
        }
        if (this._clickFeature) {
            const _properties = this._clickFeature.properties;
            const alarmArr = JSON.parse(_properties.alarm);

            let alarm = null;
            for (let i = 0; i < alarmArr.length; i++) {
                if (parseInt(alarmArr[i]['w6']) == _properties.alarm_lev) {
                    alarm = alarmArr[i];
                    break;
                }
            }


            let el = document.createElement('div');
            el.className = 'function-detail';

            let dev0 = document.createElement('div');
            dev0.className = 'half-top';
            el.appendChild(dev0);

            let dev1 = document.createElement('div');
            dev1.className = 'half-bottom';
            dev1.innerHTML = alarm['w9'] || '';
            el.appendChild(dev1);

            let dev2 = document.createElement('div');
            dev2.className = 'function-img';
            let warnType =parseInt(alarm['w4']);
            let warnLevel =parseInt(alarm['w6']);
            if (warnType == '01') {
                if (warnLevel == '01') {     //台风
                    dev2.className = 'img01';    //蓝
                }else if (warnLevel == '02') {
                    dev2.className = 'img02';    //黄
                }else if (warnLevel == '03') {
                    dev2.className = 'img03';    //橙
                }else if (warnLevel == '04') {
                    dev2.className = 'img04';    //红
                }
            }else if (warnType == '02') {    //暴雨
                if (warnLevel == '01') {
                    dev2.className = 'img05';
                }else if (warnLevel == '02') {
                    dev2.className = 'img06';
                }else if (warnLevel == '03') {
                    dev2.className = 'img07';
                }else if (warnLevel == '04') {
                    dev2.className = 'img08';
                }
            }else if (warnType == '03') {    //暴雪
                if (warnLevel == '01') {
                    dev1.className = 'img09';
                }else if (warnLevel == '02') {
                    dev2.className = 'img10';
                }else if (warnLevel == '03') {
                    dev2.className = 'img11';
                }else if (warnLevel == '04') {
                    dev2.className = 'img12';
                }
            }else if (warnType == '04') {     //寒潮
                if (warnLevel == '01') {
                    dev2.className = 'img13';
                }else if (warnLevel == '02') {
                    dev2.className = 'img14';
                }else if (warnLevel == '03') {
                    dev2.className = 'img15';
                }else if (warnLevel == '04') {
                    dev2.className = 'img16';
                }
            }else if (warnType == '05') {    //大风
                if (warnLevel == '01') {
                    dev2.className = 'img17';
                }else if (warnLevel == '02') {
                    dev2.className = 'img18';
                }else if (warnLevel == '03') {
                    dev2.className = 'img19';
                }else if (warnLevel == '04') {
                    dev2.className = 'img20';
                }
            }else if (warnType == '06') {     //沙尘暴
                if (warnLevel == '02') {
                    dev2.className = 'img21';
                }else if (warnLevel == '03') {
                    dev2.className = 'img22';
                }else if (warnLevel == '04') {
                    dev2.className = 'img23';
                }
            }else if (warnType == '07') {     //高温
                if (warnLevel == '02') {
                    dev2.className = 'img24';
                }else if (warnLevel == '03') {
                    dev2.className = 'img25';
                }else if (warnLevel == '04') {
                    dev2.className = 'img26';
                }
            }else if (warnType == '08') {     //干旱
                if (warnLevel == '03') {
                    dev2.className = 'img27';
                }else if (warnLevel == '04') {
                    dev2.className = 'img28';
                }
            }else if (warnType == '09') {     //雷电
                if (warnLevel == '02') {
                    dev2.className = 'img29';
                }else if (warnLevel == '03') {
                    dev2.className = 'img30';
                }else if (warnLevel == '04') {
                    dev2.className = 'img31';
                }
            }else if (warnType == '10') {      //冰雹
                if (warnLevel == '03') {
                    dev2.className = 'img32';
                }else if (warnLevel == '04') {
                    dev2.className = 'img33';
                }
            }else if (warnType == '11') {      //霜冻
                if (warnLevel == '01') {
                    dev2.className = 'img34';
                }else if (warnLevel == '02') {
                    dev2.className = 'img35';
                }else if (warnLevel == '03') {
                    dev2.className = 'img36';
                }
            }else if (warnType == '12') {      //大雾
                if (warnLevel == '02') {
                    dev2.className = 'img37';
                }else if (warnLevel == '03') {
                    dev2.className = 'img38';
                }else if (warnLevel == '04') {
                    dev2.className = 'img39';
                }
            }else if (warnType == '13') {        //霾
                if (warnLevel == '02') {
                    dev2.className = 'img44';
                }else if (warnLevel == '03') {
                    dev2.className = 'img40';
                }
            }else if (warnType == '14') {         //道路结冰
                if (warnLevel == '02') {
                    dev2.className = 'img41';
                }else if (warnLevel == '03') {
                    dev2.className = 'img42';
                }else if (warnLevel == '04') {
                    dev2.className = 'img43';
                }
            }
            dev0.appendChild(dev2);

            // let dev2 = document.createElement('div');
            // dev2.className = 'function-img';
            // dev0.appendChild(dev2);

            let dev3 = document.createElement('div');
            dev3.className = 'half-top-right';
            dev0.appendChild(dev3);

            let dev4 = document.createElement('div');
            dev4.className = 'function-title';
            dev4.innerHTML = `<span>${alarm['w13']}</span>`;
            dev3.appendChild(dev4);

            let dev5 = document.createElement('div');
            dev5.className = 'function-time';
            dev5.innerHTML = `<span>发布时间：${alarm['w8']}</span>`;
            dev3.appendChild(dev5);

            let dev7 = document.createElement('div');
            dev7.className = 'function-roadname-title';
            dev7.innerHTML = '可能影响道路：';
            el.appendChild(dev7);

            let dev6 = document.createElement('div');
            dev6.className = 'function-roadname';
            let roads = _properties.ref_names;
            roads = roads.replace(/;/g, '，');
            roads = roads.replace(/None/g, '无');
            dev6.innerHTML = roads;
            el.appendChild(dev6);
            
            // let lng = e.lngLat.lng;
            // let lat = e.lngLat.lat;
            // console.log(lng,lat);
            // minemap.util.getJSON('http://203.81.245.38:48082/weather/RoadNameSearch/'+lng+'/'+lat+'/1000', function (error,res) {
            //     console.log(res.data.rows[0].RoadName);
            //     roadname = res.data.rows[0].RoadName.toString();
            //     // roadname.toString();
            //     console.log(roadname);
            //     console.log(123);
            //     dev6.innerHTML = '可能影响道路：' + roadname;
            // });
            this._marker = new minemap.Popup({
                closeOnClick: true,
                closeButton: false,
                offset: [0, 0]
            }).setLngLat(this._clickFeature.geometry.coordinates).setDOMContent(el).addTo(map);
        }
    }

    // setVisible(map) {
    //     map.setLayoutProperty('5a3ea454892d4824b622cdabb48950a2', 'visibility', 'visible');
    //     this._clear(map);
    // }
    //
    // setUnvisible(map) {
    //     map.setLayoutProperty('5a3ea454892d4824b622cdabb48950a2', 'visibility', 'none');
    //     this._clear(map);
    // }
    //
    // _clear(map) {
    //     if (map) {
    //         if (this._marker) {
    //             this._marker.remove();
    //         }
    //         if (this._popup) {
    //             this._popup.remove();
    //         }
    //     }
    //     this._marker = null;
    //     this._popup = null;
    //     this._moveFeature = null
    //     this._clickFeature = null
    // }
}